home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / PRTITION.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  1KB  |  54 lines

  1.  #include<functional>
  2.  #include<deque>
  3.  #include<algorithm>
  4.  
  5.  using namespace std;
  6.  
  7.  //
  8.  // Create a new predicate from unary_function.
  9.  //
  10.  template<class Arg>
  11.  class is_even : public unary_function<Arg, bool>
  12.  {
  13.    public:
  14.    bool operator()(const Arg& arg1) { return (arg1 % 2) == 0; }
  15.  };
  16.  
  17.  int main ()
  18.  {
  19.    //
  20.    // Initialize a deque with an array of integers.
  21.    //
  22.    int init[10] = { 1,2,3,4,5,6,7,8,9,10 };
  23.    deque<int> d1(init+0, init+10);
  24.    deque<int> d2(init+0, init+10);
  25.    //
  26.    // Print out the original values.
  27.    //
  28.    cout << "Unpartitioned values: " << "\t\t";
  29.    copy(d1.begin(), d1.end(), ostream_iterator<int>(cout," "));
  30.    cout << endl;
  31.    //
  32.    // A partition of the deque according to even/oddness.
  33.    //
  34.    partition(d2.begin(), d2.end(), is_even<int>());
  35.    //
  36.    // Output result of partition.
  37.    //
  38.    cout << "Partitioned values: " << "\t\t";
  39.    copy(d2.begin(), d2.end(), ostream_iterator<int>(cout," "));
  40.    cout << endl;
  41.    //
  42.    // A stable partition of the deque according to even/oddness.
  43.    //
  44.    stable_partition(d1.begin(), d1.end(), is_even<int>());
  45.    //
  46.    // Output result of partition.
  47.    //
  48.    cout << "Stable partitioned values: " << "\t";
  49.    copy(d1.begin(), d1.end(), ostream_iterator<int>(cout," "));
  50.    cout << endl;
  51.  
  52.    return 0;
  53.  }
  54.